package com.dhcc.core.modules.system.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.config.ConfigCache;
import com.dhcc.core.modules.system.cache.config.IConfigCache;
import com.dhcc.core.modules.system.entity.ConfigCategory;
import com.dhcc.core.modules.system.service.IConfigCategoryService;
import com.dhcc.core.modules.system.warpper.ConfigCategoryWarpper;


/**
 * 参数分类
 * 
 * @ClassName: ConfigCategoryController
 * @Description: TODO
 * @author cyf
 * @date 2018-11-26 19:28:37
 */
@Controller
@RequestMapping("${sys.backendPath}/config/category")
public class ConfigCategoryController extends BaseController {

	private String PREFIX = "/backend/system/config/";

	@Autowired
	private IConfigCategoryService configCategoryService;
    /**
     * 跳转到新增参数分类
     */
    @RequestMapping("/to_add")
    @Permission("config:category:add")
    public String toAdd() {
        return PREFIX + "category_add.html";
    }
	
	/**
     * 跳转到修改参数分类
     */
    @RequestMapping("/to_update/{configCategoryId}")
    @Permission("config:category:update")
    public String toUpdate(@PathVariable Long configCategoryId, Model model) {
        ConfigCategory configCategory = this.configCategoryService.selectById(configCategoryId);
        model.addAttribute("configCategory",new ConfigCategoryWarpper(configCategory).warp());
        return PREFIX + "category_edit.html";
    }

    /**
     * 新增参数分类
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("config:category:add")
    @BussinessLog(value = "新增参数分类")
    @CacheClear(IConfigCache.CACHE_NAME)
    public Object add(ConfigCategory configCategory) {
        if (CommonUtil.isOneEmpty(configCategory)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        configCategory.init();
        configCategory.insert();
        return R.ok();
    }

    /**
     * 删除参数分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("config:category:delete")
    @BussinessLog(value = "删除参数分类")
    @CacheClear(IConfigCache.CACHE_NAME)
    public Object delete(@RequestParam Long configCategoryId) {
	
        this.configCategoryService.deleteById(configCategoryId);

        return R.ok();
    }

    /**
     * 修改参数分类
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("config:category:update")
    @BussinessLog(value = "修改参数分类")
    @CacheClear(IConfigCache.CACHE_NAME)
    public Object update(ConfigCategory configCategory) {
        if (CommonUtil.isOneEmpty(configCategory)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        ConfigCategory old = this.configCategoryService.selectById(configCategory.getId());

        old.setName(configCategory.getName());
        old.setParentId(configCategory.getParentId());
        old.setDescription(configCategory.getDescription());
        old.init();
        old.setWeight(configCategory.getWeight());
        old.updateById();
        return R.ok();
    }

    /**
     * 获取分类的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> categoryTree() {
        return ConfigCache.me().getCategoryTree();
    }
	
}
